package com.fengke.cn.yejian11.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.fengke.cn.yejian11.R;
import com.fengke.cn.yejian11.base.BaseActivity;
import com.fengke.cn.yejian11.config.Config;
import com.fengke.cn.yejian11.fragment.ClubCourseFragment;
import com.fengke.cn.yejian11.fragment.ClubIntroductionFragment;
import com.fengke.cn.yejian11.fragment.ClubVideoFragment;
import com.fengke.cn.yejian11.impls.ActImpls;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ClubInfoActivity extends BaseActivity {
    private ImageView imageView;
    private RadioGroup radioGroup;
    private ClubCourseFragment clubCourseFragment;
    private ClubIntroductionFragment clubIntroductionFragment;
    private ClubVideoFragment clubVideoFragment;
    private FragmentManager manager;
    private FragmentTransaction transaction;
    private ImageView state;

    private TextView Club_Name;
    private ImageView Club_Pic;
    private String Club_Name_xml;
    private String Club_Name_Pic;
    private String fan_id;
    public String userid;
//    private String userid="97";
    private String fans_type="1";
    private String SessonID="pf2cmg5jk9p24f2mv1kjf55bo3";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club_info);

        userid=getUserId();

        fan_id=getIntent().getStringExtra("Club_Id");
        Club_Name_Pic=getIntent().getStringExtra("Club_Pic");
        Club_Name_xml=getIntent().getStringExtra("Club_Name");
        state= (ImageView) findViewById(R.id.club_state);
        imageView= (ImageView)findViewById(R.id.imageView4);
        radioGroup= (RadioGroup)findViewById(R.id.radioGroup);
        Club_Name= (TextView) findViewById(R.id.club_radio_name);
        Club_Name.setText(Club_Name_xml);
        Club_Pic= (ImageView) findViewById(R.id.club_radio_pic);
        Picasso.with(ClubInfoActivity.this)
                .load(Club_Name_Pic)
                .into(Club_Pic);

        getClubState(fan_id,userid,fans_type,SessonID);
        //////////////////////////////////////////////////////////////////////////
        state.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Retrofit retrofit=new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl(Config.URL).build();
                ActImpls servise=retrofit.create(ActImpls.class);
                Call<Integer> call=servise.changeClubState(userid,fan_id,fans_type);
                call.enqueue(new Callback<Integer>() {
                    @Override
                    public void onResponse(Call<Integer> call, Response<Integer> response) {
                        if (response.body()!=0){
                            Toast.makeText(ClubInfoActivity.this,"关注成功",Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<Integer> call, Throwable t) {

                    }
                });
            }
        });
        //

        ///////////////////////////////////////////////////////////////////////////
        manager=getSupportFragmentManager();
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                switch (checkedId){

                    case R.id.radio_club_info:
                        clubIntroductionFragment=new ClubIntroductionFragment();
                        replace(clubIntroductionFragment);
                        break;
                    case R.id.radio_club_course:
                        clubCourseFragment=new ClubCourseFragment();
                        replace(clubCourseFragment);
                        break;
                    case R.id.radio_club_video:
                        clubVideoFragment=new ClubVideoFragment();
                        replace(clubVideoFragment);
                        break;
                }
            }
        });

        radioGroup.check(R.id.radio_club_info);

    }

    private void replace(Fragment fragment){
        transaction=manager.beginTransaction();
        transaction.replace(R.id.clubinfo_framelayout,fragment);
        transaction.commit();
    }
////////////////////////////////////////////////////////////////////////////////////////////////
    private void getClubState(String fanid,String user_id,String fanstype,String SessonId){

        Retrofit retrofit=new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl(Config.URL).build();
        ActImpls servise=retrofit.create(ActImpls.class);
        Call<Integer> call=servise.getClubState(fanid,user_id,fanstype,SessonId);
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if (response.body()==0){
                    state.setImageResource(R.drawable.ic_favor);
                }else {
                    state.setImageResource(R.drawable.ic_favor_unchecked);
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {

            }
        });
    }
}
